<?php

namespace App\Models;

use App\Exceptions\ParamValidateFailedException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Info extends Model
{

    const
        //通知状态
        INFO_STATUS_UNWATCHED = 0,
        INFO_STATUS_WATCHED = 1,
        //通知类型
        INFO_TYPE_ORDER = 0,
        INFO_TYPE_INCOME = 1,
        INFO_TYPE_DIY = 2,
        INFO_TYPE_ALL = 3,  //查询用
        //通知标题
        INFO_ORDER_TITLE = '您有新的订单信息',
        INFO_INCOME_TITLE = '您有一笔新的收入',
        INFO_OTHERS_OTHERS = '您收到了一条信息';

    public static function CreateInfoModel($infos){
        foreach ($infos as $info){
            $type = $info['type'];
            $owner = $info['owner'];
            $content = $info['content'];
            isset($info['data']) ? $data = $info['data'] : $data = NULL;

            switch ($type){
                case self::INFO_TYPE_ORDER:
                    $title = self::INFO_ORDER_TITLE;
                    break;
                case self::INFO_TYPE_INCOME:
                    $title = self::INFO_INCOME_TITLE;
                    break;
                case self::INFO_TYPE_DIY:
                    if (!empty($data)){
                        $title = $data;
                    } else{
                        throw new ParamValidateFailedException();
                    }
                    break;
                default:
                    $title = self::INFO_OTHERS_OTHERS;
                    break;
            }

            DB::table('infos')->insert(['title'=>$title,'content'=>$content
                    ,'type'=>$type,'owner'=>$owner,'status'=>self::INFO_STATUS_UNWATCHED,
                'created_at'=>date('Y-m-d H:i:s',time()),
                'updated_at'=>date('Y-m-d H:i:s',time())]);
        }

    }

    public static function InfoType($infos, $type = self::INFO_TYPE_ALL){
        switch ($type){
            case self::INFO_TYPE_ORDER:
                $infos = $infos->where('type', self::INFO_TYPE_ORDER);
                break;
            case self::INFO_TYPE_INCOME:
                $infos = $infos->where('type', self::INFO_TYPE_INCOME);
                break;
            case self::INFO_TYPE_DIY:
                $infos = $infos->where('type', self::INFO_TYPE_DIY);
                break;
            default:
                break;
        }
        return $infos;
    }


}
